#!/usr/bin/env python
"""A simplistic, prototype of a Python tail utility"""

from optparse import OptionParser
from collections import deque

__version__ = "0.1"

def ntail(filename, number = 10, lines = False):
    'Return the last number lines of a file, numbered or not'
    tail = deque(open(filename), number)
    if lines:
        for n, w in enumerate(tail):
            print n,w.strip()
    else:
        for line in tail:
            print line.strip()

def controller():
    description_message = "A Python version of tail"
    p = OptionParser(description=description_message,
                prog='snaketail',
                version='liten %s' % __version__,
                usage= '%prog [options] [file]')
    p.add_option('--lines', '-l', action="store_true",
                    help='prints line numbers',default=False)
    p.add_option('--number', '-m', action="store_true",
                    help='print a number of lines',default=False)
    options, arguments = p.parse_args()
    if arguments:
        try:
            ntail(filename = arguments[0], lines = options.lines)
        except Exception, err:
            print err
            p.print_help()
    else:
        p.print_help()
if __name__ == "__main__":
    controller()
    